<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-card">
        <div class="login-header">
          <h1>欢迎登录</h1>
          <p>请输入您的用户名和密码以继续</p>
        </div>

        <!-- 登录表单 -->
        <el-form :model="form" :rules="rules" ref="formRef"  @keyup.enter.native="submitForm">
          <!-- 用户名 -->
          <el-form-item prop="username" class="form-item">
            <el-input
                v-model="form.username"
                placeholder="用户名"
                prefix-icon="el-icon-user"
                class="input-field"
            />
          </el-form-item>

          <!-- 密码 -->
          <el-form-item prop="password" class="form-item">
            <el-input
                v-model="form.password"
                type="password"
                placeholder="密码"
                prefix-icon="el-icon-lock"
                show-password
                class="input-field"
            />
          </el-form-item>

          <!-- 登录按钮 -->
          <el-form-item class="button-group">
            <el-button type="primary" @click="submitForm" class="login-btn">
              登录
            </el-button>
          </el-form-item>
        </el-form>

        <div class="login-footer">
          <p>还没有账号？<a @click="goToRegister">点击注册</a></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {ref, reactive} from 'vue';
import {ElMessage, ElNotification} from 'element-plus';
import {useRoute, useRouter} from "vue-router";
import {apiPost} from "@/utils/api";
import {getTime} from "@/utils/time";
import {sessionStorage} from "@/utils/storage";

//获取路由器
let $router = useRouter();
//路由对象
let $route = useRoute();
// 表单数据
const form = reactive({
  username: '',
  password: '',
  type: 1,
});

// 表单验证规则
const rules = reactive({
  username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}],
});

// 表单引用
const formRef = ref();

// 提交表单
const submitForm = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      const {data} = await apiPost("/user/login", {...form});
      sessionStorage.set("token", data);
      let redirect = $route.query.redirect;
      await $router.push({path: redirect as string || "/"});
      //登录成功提示信息
      ElNotification({
        type: "success",
        message: `欢迎回来管理员${form.username}`,
        title: `HI,${getTime()}好`,
      });
    } else {
      ElMessage.warning('请填写完整信息');
    }
  });
};

// 跳转到注册页面
const goToRegister = () => {
  $router.push('/register');
};
</script>

<style scoped>
/* 页面容器 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('https://source.unsplash.com/1600x900/?nature,forest'); /* 背景图 */
  background-size: cover;
  background-position: center;
}

/* 登录卡片容器 */
.login-wrapper {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 400px;
  text-align: center;
}

/* 登录卡片 */
.login-card {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 登录标题 */
.login-header h1 {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  font-size: 14px;
  color: #666;
}

/* 表单项 */
.form-item {
  margin-bottom: 20px;
}

.input-field {
  width: 100%;
  border-radius: 8px;
}

/* 按钮组 */
.button-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.login-btn,
.register-btn {
  width: 100%;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.login-btn {
  background-color: #6a11cb;
  border: none;
  color: white;
}

.login-btn:hover {
  background-color: #2575fc;
}

/* 底部链接 */
.login-footer {
  font-size: 14px;
  color: #666;
}

.login-footer a {
  color: #6a11cb;
  text-decoration: none;
}

.login-footer a:hover {
  text-decoration: underline;
}
</style>